<template>
    <div class="app-container">
        <div style="text-align: center;margin-bottom: 30px">
            消息通知{{createStatus?'新增':'更新'}}
        </div>
        <el-form :model="noticeFrom"  ref="noticeFrom" :rules="noticeFromRules" label-width="100px">
            <el-row :gutter="10">
                <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item  label="所属主体" prop="enterpriseId">
                            <el-select v-model="noticeFrom.enterpriseId" placeholder="请选择主体" style="width: 100%"  @change="getInUserList" v-if="enterPriseHidden">
                                <el-option v-for="(item,index) in enterpriseDominantList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="通知名称" prop="name">
                            <el-input type="text" v-model="noticeFrom.name" placeholder="请输入通知名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="通知内容"  prop="content">
                            <el-input v-model="noticeFrom.content" placeholder="请输入通知内容"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="通知类型 " prop="type">
                            <el-select v-model="noticeFrom.type" placeholder="通知类型" style="width: 100%">
                                <el-option :value="0" label="系统通知"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="接收人 " prop="receiverIds">
                            <el-select v-model="noticeFrom.receiverIds" multiple placeholder="通知类型" style="width: 100%">
                                <el-option v-for="(item,index) in userList" :key="index"  :value="item.ucId" :label="item.disPlayName"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-col>
            </el-row>
        </el-form>

        <div slot="footer" style="text-align: center" class="dialog-footer">
            <el-button @click="cancelnoticeFrom">取 消</el-button>
            <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createnoticeFrom">创 建</el-button>
            <el-button :loading="addLoading" v-else type="primary" @click="updatenoticeFrom">修 改</el-button>
        </div>
    </div>
</template>

<script>
import {addInNotice, getInNoticeVo, updateInNotice} from "@/api/notice";
import {getInUserList} from "@/api/enterprise";
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";


export default {
    name: "InnoticeFromEdit",
    data(){
        return{
            noticeFrom:{
                receiverIds:[],
            },
            noticeFromRules:{
                type: [{required: true, message: '通报类型不能为空', trigger: 'blur'}],
                receiver: [{required: true, message: '详细地址不能为空', trigger: 'blur'}],
                name: [{required: true, message: '通知名称不能为空', trigger: 'blur'}],

            },
            addLoading:false,
            userList:[],
            enterpriseDominantList:[],
        }
    },
    computed:{
        createStatus(){
            return this.$route.params.ucId == '0'
        },
      enterPriseHidden(){
        let roleCode = this.$store.getters.roleCode;
        return roleCode=="ADMIN"||roleCode=="XT-ADMIN"||roleCode=="ZD-ADMIN"||roleCode=="DD-ADMIN"||roleCode=="JW-ADMIN"||roleCode=="XZ-ADMIN"
      }
    },
    created() {
    this.getInEnterpriseDominantList()
        console.log(this.createStatus)
        if (!this.createStatus){
            let ucId = this.$route.params.ucId
            console.log(ucId)
            this.getInNoticeVo(ucId)
        }

    },
    methods:{
        getInNoticeVo(val){
            getInNoticeVo({ucId:val}).then(res=>{
                console.log(res.data)
                this.noticeFrom = res.data
                this.getInUserList(res.data.enterpriseId)
            }).catch(res=>{

            })
        },
        cancelnoticeFrom(){
          window.history.back()
        },
        createnoticeFrom(){
            this.addLoading = true
            this.$refs['noticeFrom'].validate(valid => {
                if (valid){
                    addInNotice(this.noticeFrom).then(res=>{
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "添加通知成功"
                        })
                        this.cancelnoticeFrom()
                    }).catch(res=>{
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                }else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        updatenoticeFrom(){
            this.addLoading = true
            this.$refs['noticeFrom'].validate(valid => {
                if (valid){
                    updateInNotice(this.noticeFrom).then(res=>{
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "更新消息通知信息成功"
                        })
                        this.cancelnoticeFrom()
                    }).catch(res=>{
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                }else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        getInEnterpriseDominantList(){
            getInEnterpriseDominantList({deleted:false}).then(res=>{
                this.enterpriseDominantList = res.data
            }).catch(error=>{

            })
        },
        getInUserList(val){
            getInUserList({deleted:false,flagId:val}).then(res=>{
                this.userList = res.data
            }).catch(error=>{

            })
        }
    }

}
</script>



